import multiprocessing
from tendo.singleton import SingleInstance
from road_gps import gpsSever
from gps_transfer import gpsTransferSever
from road_detect import detectSever
from result_transfer import resultTransferSever
from utils.gps_device import recv_cyc
from utils.config import gps_run, videos_run

try:
    Single = SingleInstance()
except:
    print('main process already  exist! ')
    exit()

if __name__ == '__main__':
    print("start Running...")
    dict = multiprocessing.Manager().dict()
    recv_cyc = multiprocessing.Process(target=recv_cyc, args=(dict,))
    gpsProc = multiprocessing.Process(target=gpsSever, args=(dict,))
    gpsTransferProc = multiprocessing.Process(target=gpsTransferSever)
    detectProc = multiprocessing.Process(target=detectSever, args=(dict,))
    detectTransferProc = multiprocessing.Process(target=resultTransferSever)

    recv_cyc.start()
    if gps_run:
        gpsProc.start()
        gpsTransferProc.start()
    if videos_run:
        detectProc.start()
        detectTransferProc.start()

    recv_cyc.join()
    if gps_run:
        gpsProc.join()
        gpsTransferProc.join()
    if videos_run:
        detectProc.join()
        detectTransferProc.join()

